﻿using Kendo.Mvc.Extensions;
using Kendo.Mvc.UI;
using OA.Models;
using System;
using System.Linq;
using System.Web.Mvc;

namespace OA.Controllers
{
    /// <summary>
    ///  岗位控制器
    /// </summary>
    public class PostController : BaseController
    {
        /// <summary>
        ///  岗位管理页面
        /// </summary>
        /// <returns></returns>
        public ActionResult Index()
        {
            return View();
        }

        /// <summary>
        ///  读取岗位数据
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [AcceptVerbs(HttpVerbs.Post)]
        public JsonResult Read([DataSourceRequest] DataSourceRequest request)
        {
            var query = Context.Posts;

            return Json(query.ToDataSourceResult(request));
        }

        /// <summary>
        ///  读取岗位数据
        /// </summary>
        /// <returns></returns>
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult DropDownListRead()
        {
            var query = Context.Posts;

            return Json(query, JsonRequestBehavior.AllowGet);
        }

        /// <summary>
        ///  新增 & 编辑岗位页
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        public ActionResult Update(int Id)
        {
            var viewModel = Context.Posts.Find(Id);

            if (viewModel == null)
            {
                viewModel = Context.Posts.Create();
            }

            return PartialView(viewModel);
        }

        /// <summary>
        ///  新增 & 编辑岗位
        /// </summary>
        /// <param name="model">岗位模型</param>
        /// <returns></returns>
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Update(Post model)
        {
            var entity = Context.Posts.Find(model.Id);
            var isNew = entity == null;

            if (isNew)
            {
                entity = Context.Posts.Create();

                entity.Name = model.Name;
                entity.Sort = model.Sort;
                entity.Grade = model.Grade;
                entity.Remarks = model.Remarks;

                Context.Posts.Add(entity);
            }
            else
            {
                if (Context.Posts.Any(d => d.Name.Equals(model.Name) && d.Id != model.Id))
                {
                    return Json(Failed("已经存在此岗位名!"));
                }

                entity.Name = model.Name;
                entity.Sort = model.Sort;
                entity.Grade = model.Grade;
                entity.Remarks = model.Remarks;
            }

            try
            {
                Context.SaveChanges();
                return Json(Success(isNew ? "添加成功!" : "更新成功!"));
            }
            catch (Exception ex)
            {
                return Json(Success("操作失败:" + ex.Message));
            }
        }

        /// <summary>
        ///  删除岗位
        /// </summary>
        /// <param name="Id">岗位Id</param>
        /// <returns></returns>
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Delete(int Id)
        {
            var entity = Context.Posts.Find(Id);

            if (entity == null)
            {
                return Json(Failed("岗位获取失败!"));
            }

            if (entity.Employees.Count != 0)
            {
                return Json(Failed("该岗位还存在在职员工, 无法删除!"));
            }

            try
            {
                Context.Posts.Remove(entity);
                Context.SaveChanges();
                return Json(Success("删除成功!"));
            }
            catch (Exception ex)
            {
                return Json(Success("删除失败:" + ex.Message));
            }
        }
    }
}